<?php
class PaidcontractsController extends AppController {

	var $name = 'Paidcontracts';
	var $helpers = array('Html', 'Form');

		function index() {
			$this->loadModel('Facultet');
			$facultets = $this->Facultet->find('list');
			$this->set(compact('facultets'));				
		}

		/**
		* @author Adm
		*	для ajax обновляет select с профессиями по выбранному факультету
		*/
		function update_profession() {
			$this->loadModel('Profession');
			if(!empty($this->data['Paidcontract']['facultet_id'])) {
				$facultet_id = $this->data['Paidcontract']['facultet_id'];
				$options = $this->Profession->find('list', array('conditions' => array ('Profession.facultet_id' => $facultet_id)));
				$this->set(compact('options'));
			}
		}
				
		function listpaid() {
			$profession_id = $this->data['Paidcontract']['profession_id'];
			$this->set('profession_id', $profession_id);
			
			$alreadypdaid = $this->Paidcontract->find('all', array('conditions' => array('Paidcontract.profession_id' => $profession_id)));
			//debug($alreadypdaid);
			$ids = array();
			foreach ($alreadypdaid as $paided):
			array_push($ids, $paided['Paidcontract']['abiturient_id']);
			endforeach;
			$id_not_in_ids = array('NOT' => array('Selectedprofession.abiturient_id' => $ids));
			
			$this->loadModel('Selectedprofession');
			$selectedprofessions = $this->Selectedprofession->find('all', array('conditions' => array($id_not_in_ids, 'Selectedprofession.profession_id' => $profession_id, 'Selectedprofession.learningtype_id' => 2), 'recursive' => -1,));
			$abiturient_ids = array();
			//debug($selectedprofessions);
			foreach ($selectedprofessions as $selectedprofession):
			array_push($abiturient_ids, $selectedprofession['Selectedprofession']['abiturient_id']);
			endforeach;
			//debug($abiturient_ids);
			$this->loadModel('Abiturient');
			$abiturients = $this->Abiturient->find('all', array('conditions' => array('Abiturient.id' => $abiturient_ids),
																					'recursive' => -1,
																					'order' => 'Abiturient.lastname',
																					'fields' => array ('Abiturient.id', 
																										'Abiturient.lastname', 
																										'Abiturient.firstname', 
																										'Abiturient.surname')));
			$this->set('abiturients', $abiturients)	;
		}
		
		function setpaid() {
		//	debug ($this->data);
			$profession_id = $this->data['profession_id'];
	//		debug($profession_id);
			$abiturients = $this->data['abiturients'];
			$abiturients_ids = array();
			
			$this->loadModel('Abiturient');
			
			foreach($abiturients as $key => $value):
			if ($value){
			
				array_push($abiturients_ids, $value);
			
				$this->Paidcontract->create();
					
				$newdata = array('Paidcontract' => array('abiturient_id' => $value,
																		'profession_id' => $profession_id,
																		'paid' => 1
				));
					
				$this->Paidcontract->save($newdata);
			}
			endforeach;			
			
			$paidabiturients = $this->Abiturient->find('all', array('conditions' => array('Abiturient.id' => $abiturients_ids,),
																						'recursive' => -1,
																						'fields' => array ('Abiturient.id', 
																											'Abiturient.lastname', 
																											'Abiturient.firstname', 
																											'Abiturient.surname')));
			$this->set('abiturients', $paidabiturients)	;			
		}
		
		function listfacultetpaid() {
			Configure::write('debug', 0);
			//debug($this->data);
			$profession_id = $this->data['Paidcontract']['profession_id'];
			$this->set('profession_id', $profession_id);
			
			$this->loadModel('Profession');
			$viewprofessions = $this->Profession->find('list');
			$this->set('professions', $viewprofessions);
			
			$alreadypdaid = $this->Paidcontract->find('all', array('conditions' => array('Paidcontract.profession_id' => $profession_id)));
			//debug($alreadypdaid);
			$paid_ids = array();
			foreach ($alreadypdaid as $paided):
			array_push($paid_ids, $paided['Paidcontract']['abiturient_id']);
			endforeach;
			
			
			$facultet_id = $this->data['Paidcontract']['facultet_id'];
			$this->set('facultet_id', $facultet_id);

			
			$this->loadModel('Facultet');
			$facultets = $this->Facultet->find('list');

			$this->set(compact('facultets'));
			
			$this->loadModel('Privilege');
			$privelegeslist = $this->Privilege->find('list');
			$this->set('privilegeslist',$privelegeslist);
			
			$this->loadModel('Profession');
			$professions = $this->Profession->find('list');
			$this->set(compact('professions'));
			
			// получаем id предметов по специальности
			$subjects_ids = $this->Profession->SubjectsProfession->find('all', array('conditions' => array('SubjectsProfession.profession_id' => $profession_id)));
			
			$profession_subjects = array();
			foreach ($subjects_ids as $subject):
			array_push($profession_subjects, $subject['SubjectsProfession']['subject_id']);
			endforeach;
			
			$this->set('subjects_ids', $profession_subjects);
			
			$this->loadModel('Region');
			$regions = $this->Region->find('list');
			$this->set('regions', $regions);
			
			$this->loadModel('Subject');
			$subjects = $this->Subject->find('list');
			$this->set('subjects', $subjects);
			
			$this->loadModel('Tgintakeorg');
			$tgintakeorgs = $this->Tgintakeorg->find('all' , array('recursive' => -1));
			$tgintakeorgslist = $this->Tgintakeorg->find('list');
			$this->set('tgintakeorgslist', $tgintakeorgslist);
			

			
			$this->loadModel('Abiturient');
			
			// выбираем инфо по абитуриентам
				
			$abiturients = $this->Abiturient->find('all', array('conditions' => array('Abiturient.id' => $paid_ids,
																															'Abiturient.prepared' => 1,
																															'Abiturient.approved' => 1,
																															'Abiturient.canceled' => 0),
																									'recursive' => 1,
																									'fields' => array ('Abiturient.id', 
																														'Abiturient.lastname', 
																														'Abiturient.firstname', 
																														'Abiturient.surname', 
																														'Abiturient.originaleducationdoc',)));		
				
			// считаем сумму баллов по специальности
			$score_total_abiturients = array(); // к данным абитуриента добавлен елемент Score_total
			foreach ($abiturients as $abiturient):
			// считаем  сумму балов по специальности
			$score_total = 0;
			foreach ($subjects_ids as $subject_id):
			foreach ($abiturient['Result'] as $result):
			if ($subject_id['SubjectsProfession']['subject_id'] == $result['subject_id']){
				$score_total += $result['score'];
			}
			endforeach;
			endforeach;
			$abiturient['Score_total'] = $score_total;
			array_push($score_total_abiturients, $abiturient);
			endforeach;
			
			// начинаем разбирать абитуриентов в  массив с сортровкой по рейтингу
			$plainrating_abiturients = array();
			foreach ($score_total_abiturients as $abiturient):
			$plainrating_abiturients[$abiturient['Abiturient']['id']] = $abiturient['Score_total'];
			endforeach;
			// сортируем массив целивиков по убыванию	Score_total
			arsort($plainrating_abiturients);
				
			$this->set('plainrating_abiturients',$plainrating_abiturients);
				
			$abiturients = $this->Abiturient->find('all', array('conditions' => array('Abiturient.id' => $paid_ids,
																																					'Abiturient.prepared' => 1,
																																					'Abiturient.approved' => 1,
																																					'Abiturient.canceled' => 0,
			),
																						'recursive' => 1,
																						'fields' => array ('Abiturient.id', 
																											'Abiturient.lastname', 
																											'Abiturient.firstname', 
																											'Abiturient.surname', 
																											'Abiturient.originaleducationdoc',
																											'Abiturient.region_id')));
			$this->set('abiturients', $abiturients)	;
			$this->render('/','blank','listfacultetpaid');			
			
		}
}